package com.xrit.sunnyday.controller;

import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.constant.RedisMessageConstant;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.utils.SMSUtils;
import com.xrit.sunnyday.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    public JedisPool jedisPool;

    /**
     * 发送验证码
     * @param telephone
     * @return
     */
    @PostMapping("/send4Order")
    public Result send4Order(String telephone){
        /*Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        System.out.println("validateCode = " + validateCode);
        //连接池.获取的值
        jedisPool.getResource().setex(telephone,30,validateCode.toString());
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);*/
        //获取连接池   获取连接
        Jedis jedis = jedisPool.getResource();
        //定义一个key的名称
        String key= RedisMessageConstant.SENDTYPE_ORDER+"_"+telephone;
        String codeInRedis = jedis.get(key);
        //判断验证码里面是否有值
        if (null == codeInRedis){
            //生成
            Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
            System.out.println("validateCode = " + validateCode);
            //用String[]代替腾讯云
            String[] parms= new String[]{validateCode + ""};
            try {
                //调用SMSUtils
                SMSUtils.sendSingleMessageForCode(telephone,parms);
                jedis.setex(key,5*60,validateCode+"");

                return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
            }

        }
        return new  Result(false,MessageConstant.SEND_VALIDATECODE);
    }


    @PostMapping("/send4Login")
    public Result send4Login(String telephone){
        /*Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        System.out.println("validateCode = " + validateCode);
        //连接池.获取的值
        jedisPool.getResource().setex(telephone,30,validateCode.toString());
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);*/
        //获取连接池   获取连接
        Jedis jedis = jedisPool.getResource();
        //定义一个key的名称
        String key= RedisMessageConstant.SENDTYPE_LOGIN+"_"+telephone;
        String codeInRedis = jedis.get(key);
        //判断验证码里面是否有值
        if (null == codeInRedis){
            //生成
            Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
            System.out.println("validateCode = " + validateCode);
            //用String[]代替腾讯云
            String[] parms= new String[]{validateCode + ""};
            try {
                //调用SMSUtils
                SMSUtils.sendSingleMessageForCode(telephone,parms);
                jedis.setex(key,5*60,validateCode+"");

                return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
            }

        }
        return new  Result(false,MessageConstant.SEND_VALIDATECODE);
    }
}
